package com.baomidou.mybatisplus;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import java.util.Arrays;
import java.util.List;


/**
 * MyBatis-Plus 代码自动生成器
 * mybatis-plus-generator 3.5.1-
 */
public class MyCodeGenerator_plus {

    //private static String outputDir = System.getProperty("user.dir") + "/src/main/java";
    private static String outputDir = "code";
    private static String jdbcUrl = "jdbc:mysql://81.71.129.74:61306/y_report_db?useSSL=true&useUnicode=true&characterEncoding=UTF8";
    private static String jdbcUserName = "root";
    private static String jdbcPassword = "123456";
    private static List<String> tableNames = Arrays.asList(
            "y_report_ds",
            "y_report_sql",
            "y_report_api",
            "y_report_log",
            "y_report_user"
    );

    public static void main(String[] args) {
        FastAutoGenerator.create(new DataSourceConfig.Builder(jdbcUrl, jdbcUserName, jdbcPassword))
                .globalConfig(x ->
                        x.disableOpenDir() //禁止打开输出目录
                                .author("y") // 设置作者
                                .dateType(DateType.TIME_PACK)//日期类型
                                .enableSwagger() // 开启 swagger 模式
                                //.fileOverride() // 覆盖已生成文件
                                .outputDir(outputDir) // 指定输出目录
                )
                .packageConfig(x ->
                        x.parent("com.y.report") // 设置父包名
                                .moduleName("") // 设置父包模块名
                                .controller("controller.basic") // 控制器包名
                                .service("service.basic") // Service包名
                                .serviceImpl("service.basic.impl") // Service Impl包名
                                .mapper("mapper.basic") // mapper包名
                                .xml("mapper.basic.xml") // xml包名
                                .entity("entity.basic") // 实体包名
                )
                .strategyConfig(x ->
                                x.addInclude(tableNames) // 设置需要生成的表名
                        //.addTablePrefix("t_", "c_") // 设置过滤表前缀
                )
                .strategyConfig(x ->  //controller
                        x.controllerBuilder()
                                .enableRestStyle()
                                .build()
                )
                .strategyConfig(x -> //service
                        x.serviceBuilder()
                                .convertServiceFileName(y -> y + ConstVal.SERVICE)
                                .build()
                )
                .strategyConfig(x -> //mapper
                        x.mapperBuilder()
                                .enableBaseResultMap()
                                .enableBaseColumnList()
                                .convertXmlFileName(y -> NamingStrategy.underlineToCamel(y + ConstVal.MAPPER))
                                .build()
                )
                .strategyConfig(x ->//entity
                        x.entityBuilder()
                                .enableChainModel()
                                .enableLombok()
                                .enableActiveRecord()
                                .naming(NamingStrategy.underline_to_camel)
                                .columnNaming(NamingStrategy.underline_to_camel)
                                .idType(IdType.ASSIGN_ID)
                                .build()
                )
                .templateEngine(new VelocityTemplateEngine()) // 使用Velocity引擎模板
                .execute();


    }

}
